<?php

class HomeAction extends ApiAction {
	protected $mainMod;
    protected  function _initialize() {
        $this->mainMod = D("Home");
    }
	public function index(){
		$grade = I('post.grade');
		$lat   = I('post.lat');
		$lon   = I('post.lon');
		$curpage = I('post.curpage',1);
		$data = $this->mainMod->getList();
		$this->mainMod->each($data);
		$location = $this->mainMod->sel();
		$return = $this->mainMod->coordinate($location,$lat,$lon);
		if($grade){
			$str = "grade";
		}else{
			$str = "between";
		}
		array_multisort(array_column($return,$str),SORT_ASC,$return);
		$url = "http://test.shiangou.com.cn";
		foreach($return as $key=>$vals){
			$arr[$key]= $this->mainMod->oneList($vals['merchant_id']);
			$arr[$key]['merchant_thumb'] = $url.$arr[$key]['merchant_thumb'];
			$arr[$key]['between'] =$vals['between'];
		}
		$cat = $this->mainMod->type(0);
		$page = ($curpage-1)*10;
		$slice = array_slice($arr,$page,10);
		$return = $this->datas($slice,$cat);
		
		echo json_encode($return);
	}
	public function datas($arr,$cat){
		if($arr){
			$data['state'] = 1;
			$data['msg']   = "返回成功";
			$data['datas'] = array('list'=>$arr,'cat'=>$cat,'banner'=>'http://test.shiangou.com.cn/merchant/Attachment/Home/banner.png');
		}else{
			$data['state'] = 1;
			$data['msg']   = "返回成功";
			$data['datas'] = array('list'=>array(),'cat'=>$cat,'banner'=>'http://test.shiangou.com.cn/merchant/Attachment/Home/banner.png');
		}
		return $data;
	}
	public function type(){
		$cat = I('post.cat',5);
		$lat = I('post.lat',39.827342987060547);
		$lon = I('post.lon',116.28915405273438);
		$curpage = I('post.curpage',1);
		$grade = I('post.grade');
		$arr = $this->mainMod->Merchant_type($cat);
		$coordinate = $this->mainMod->ArrGet($arr);
		$data = $this->mainMod->getList();
		$this->mainMod->each($data);
		$return = $this->mainMod->coordinate($coordinate,$lat,$lon);
		$category = $this->mainMod->category($cat);
		if($grade){
			$str = "grade";
		}else{
			$str = "between";
		}
		array_multisort(array_column($return,$str),SORT_ASC,$return);
		$url = "http://test.shiangou.com.cn";
		foreach($return as $key=>$vals){
			$array[$key]= $this->mainMod->oneList($vals['merchant_id']);
			$array[$key]['merchant_thumb'] = $url.$arr[$key]['merchant_thumb'];
			$array[$key]['between'] =$vals['between'];
		}
		$page = ($curpage-1)*10;
		$slice = array_slice($array,$page,10);
		$foot = $this->ret($slice,$category);
		echo json_encode($foot);
	}
	public function ret($arr,$category){
		if($arr){
			$data['state'] = 1;
			$data['msg']   = "返回成功";
			$data['datas'] = array('list'=>$arr,'category'=>$category);
		}else{
			$data['state'] = -1;
			$data['msg']   = "返回失败";
			$data['datas'] = [];
		}
		return $data;
	}
	public function fication(){
		$data = $this->mainMod->stage();
		if($data){
			$return['state'] = 1;
			$return['msg']   = "返回成功";
			$return['datas'] = $data;
		}else{
			$return['state'] = 1;
			$return['msg']   = "返回成功";
			$return['datas'] = [];
		}

		echo json_encode($return);
	}
	public function category(){
		$type_id = I('post.type_id',5);
		$data = $this->mainMod->category($type_id);

		if($data){
			// $data[0]['cat_id'] = 0;
			// $data[0]['cat_name'] = "全部分类";
			array_unshift($data,array('cat_id'=>0,'cat_name'=>"全部分类"));
			$return['state'] = 1;
			$return['msg']   = "返回成功";
			$return['datas'] = $data;
		}else{
			$return['state'] = 1;
			$return['msg']   = "返回成功";
			$return['datas'] = [];
		}
		echo json_encode($return);
	}

	public function searchShop(){
		$data = array();
		$data = $this->mainMod->like();
		if($data){
			$return['state'] = 1;
			$return['msg']   = "返回成功";
			$return['datas'] = $data;
		}else{
			$return['state'] = 1;
			$return['msg']   = "返回成功";
			$return['datas'] = [];
		}
		echo json_encode($return);
	}
}